package com.alliance.shuwei;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	@Qualifier("userDetailsService")
	private UserDetailsService userDetailsService;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//@formatter:off
		http.csrf().disable();
        http.authorizeRequests()
                //.antMatchers("/admin/**").authenticated()
                //.antMatchers("/admin/users/**").access("hasRole('admin')")
                //.antMatchers("/admin/roles/**").hasRole("admin")
                .anyRequest().permitAll()
                .and()
            .formLogin()
                .loginPage("/admin/login")
                .failureUrl("/admin/login?error")
                .defaultSuccessUrl("/admin", false)
                .permitAll()
                .and()
            .logout()
            	.logoutUrl("/admin/logout")
                .permitAll();
        	
      //@formatter:on
	}

	@Autowired
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
		DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
		provider.setUserDetailsService(userDetailsService);
		auth.authenticationProvider(provider);
	}

}
